package com.ruoyi.api.controller;

import com.ruoyi.biz.dto.LotteryDTO;
import com.ruoyi.biz.dto.LotteryLogDTO;
import com.ruoyi.biz.dto.LotteryPrizeDTO;
import com.ruoyi.biz.dto.LotteryResultDTO;
import com.ruoyi.biz.entity.*;
import com.ruoyi.biz.service.LotteryLogService;
import com.ruoyi.biz.service.LotteryPrizeService;
import com.ruoyi.biz.service.LotteryResultService;
import com.ruoyi.biz.service.LotteryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.util.ValidateUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@CrossOrigin
@RequestMapping("/api/lottery")
public class APILotteryController extends BaseController {

    @Resource
    LotteryService lotteryService;
    @Resource
    LotteryPrizeService lotteryPrizeService;
    @Resource
    LotteryResultService lotteryResultService;
    @Resource
    LotteryLogService lotteryLogService;

    @PostMapping("/queryCurrentLottery")
    @ResponseBody
    public AjaxResult queryCurrentLottery(LotteryDTO lotteryDTO) {
        if (ValidateUtil.isEmpty(lotteryDTO)) {
            lotteryDTO = new LotteryDTO();
            lotteryDTO.setQueryDate(new Date());
        }
        if (ValidateUtil.isEmpty(lotteryDTO.getQueryDate())) {
            lotteryDTO.setQueryDate(new Date());
        }
        lotteryDTO.setValiFlag("1");
        List<LotteryDO> list = lotteryService.queryList(lotteryDTO);
        if (ValidateUtil.isEmpty(list)) {
            return error("当前不存在抽奖信息！");
        }
        return success(list.get(0));
    }

    @PostMapping("/queryLotteryPrize")
    @ResponseBody
    public AjaxResult queryLotteryPrize(LotteryPrizeDTO lotteryPrizeDTO) {
        if (ValidateUtil.isEmpty(lotteryPrizeDTO)) {
            return error("入参不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryPrizeDTO.getLotteryId())) {
            return error("抽奖ID不能为空！");
        }
        List<LotteryPrizeDO> list = lotteryPrizeService.queryList(lotteryPrizeDTO);
        for (LotteryPrizeDO item : list) {
            item.setPrizeImgBase64String(new String(item.getPrizeImgByte()));
            //这里就不放byte了，减少响应体，加快访问速度
            item.setPrizeImgByte(null);
        }
        return success(list);
    }

    @PostMapping("/getLotteryResult")
    @ResponseBody
    public AjaxResult getLotteryResult(LotteryResultDTO lotteryResultDTO, HttpServletRequest httpServletRequest) {
        if (ValidateUtil.isEmpty(lotteryResultDTO)) {
            return error("入参不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryResultDTO.getLotteryId())) {
            return error("抽奖ID不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryResultDTO.getTel())) {
            return error("联系电话不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryResultDTO.getPsnName())) {
            return error("姓名不能为空！");
        }
        //查询抽奖活动信息
        LotteryDTO qLotteryDTO = new LotteryDTO();
        qLotteryDTO.setLotteryId(lotteryResultDTO.getLotteryId());
        qLotteryDTO.setQueryDate(new Date());
        qLotteryDTO.setValiFlag("1");
        List<LotteryDO> lotteryDTOS = lotteryService.queryList(qLotteryDTO);
        if (ValidateUtil.isEmpty(lotteryDTOS)) {
            return error("抽奖活动已过期！");
        }
        //查询抽奖记录信息
        LotteryLogDTO qLotteryLogDTO = new LotteryLogDTO();
        qLotteryLogDTO.setValiFlag("1");
        qLotteryLogDTO.setTel(lotteryResultDTO.getTel());
        qLotteryLogDTO.setPsnName(lotteryResultDTO.getPsnName());
        qLotteryLogDTO.setLotteryId(lotteryResultDTO.getLotteryId());
        List<LotteryLogDO> lotteryLogDTOS = lotteryLogService.queryList(qLotteryLogDTO);
        if (!ValidateUtil.isEmpty(lotteryLogDTOS)) {
            if (lotteryDTOS.get(0).getLotteryCntByPsn() <= lotteryLogDTOS.size()) {
                return error("您的抽奖次数已经用完！");
            }
            //如果已经中了奖，那就直接返回
            List<LotteryLogDO> filterResult = lotteryLogDTOS.stream().filter(item -> !ValidateUtil.isEmpty(item.getLotteryResultId())).collect(Collectors.toList());
            if (!ValidateUtil.isEmpty(filterResult)) {
                return error("您已经中过奖，无需再抽奖！");
            }
        }
        //查询奖品信息
        LotteryPrizeDTO qLotteryPrizeDTO = new LotteryPrizeDTO();
        qLotteryPrizeDTO.setValiFlag("1");
        qLotteryPrizeDTO.setLotteryId(lotteryResultDTO.getLotteryId());
        List<LotteryPrizeDO> lotteryPrizeDTOS = lotteryPrizeService.queryList(qLotteryPrizeDTO);
        if (ValidateUtil.isEmpty(lotteryPrizeDTOS)) {
            return error("系统出错，查询奖品信息失败！");
        }
        //定义写表实体类
        LotteryLogDTO lotteryLogDTO = new LotteryLogDTO();
        lotteryLogDTO.setLotteryId(lotteryResultDTO.getLotteryId());
        lotteryLogDTO.setTel(lotteryResultDTO.getTel());
        lotteryLogDTO.setPsnName(lotteryResultDTO.getPsnName());
        //组装基础数据
        lotteryLogDTO.setCrterId(lotteryResultDTO.getTel());
        lotteryLogDTO.setCrterName(lotteryResultDTO.getPsnName());
        lotteryLogDTO.setCrteTime(new Date());
        lotteryLogDTO.setOpterId(lotteryResultDTO.getTel());
        lotteryLogDTO.setOpterName(lotteryResultDTO.getPsnName());
        lotteryLogDTO.setOptTime(new Date());

        //查询中奖信息
        LotteryResultDTO qLotteryResultDTO = new LotteryResultDTO();
        qLotteryResultDTO.setValiFlag("1");
        qLotteryResultDTO.setLotteryId(lotteryResultDTO.getLotteryId());
        List<LotteryResultDO> lotteryResultDTOS = lotteryResultService.queryList(qLotteryResultDTO);

        //中奖flag，true代表中了，false代表没中
        boolean lotteryFlag = false;
        //能进入这个循环的，都是抽奖次数还未用完，且没抽中奖的
        for (LotteryPrizeDO lotteryPrizeDTO : lotteryPrizeDTOS) {
            //查询每个奖项的中奖信息
            List<LotteryResultDO> filterResult = lotteryResultDTOS.stream().filter(item -> lotteryPrizeDTO.getLotteryPrizeId().equals(item.getLotteryPrizeId())).collect(Collectors.toList());
            // 看看这个人是不是被添加的中奖人
            String tel = lotteryResultDTO.getTel();
            String psnName = lotteryResultDTO.getPsnName();
            List<LotteryResultDO> lotteryResultDTOSelected = filterResult.stream().filter(item -> tel.equals(item.getTel()) && psnName.equals(item.getPsnName()) && "1".equals(item.getLotteryResultType())).collect(Collectors.toList());
            //如果是被添加的中奖人，那就直接让他中
            if (!ValidateUtil.isEmpty(lotteryResultDTOSelected)) {
                lotteryFlag = true;
                lotteryLogDTO.setLotteryResultId(lotteryResultDTOSelected.get(0).getLotteryResultId());
                lotteryLogDTO.setLotteryPrizeTitle(lotteryResultDTOSelected.get(0).getLotteryPrizeTitle());
                lotteryLogDTO.setLotteryPrizeName(lotteryResultDTOSelected.get(0).getLotteryPrizeName());

                lotteryResultDTO = new LotteryResultDTO();
                BeanUtils.copyBeanProp(lotteryResultDTO, lotteryResultDTOSelected.get(0));
                lotteryResultDTO.setOpterId(tel);
                lotteryResultDTO.setOpterName(psnName);
                lotteryResultDTO.setOptTime(new Date());
                break;
            }
            //如果他不是被添加的中奖人，那就正常走中奖流程
            if (filterResult.size() < lotteryPrizeDTO.getPrizeCnt()) {
                int randomValue = (int) (Math.random() * 10000 + 1);
                if (randomValue <= lotteryPrizeDTO.getPrizeRate()) {
                    lotteryFlag = true;
                    lotteryLogDTO.setLotteryPrizeTitle(lotteryPrizeDTO.getLotteryPrizeTitle());
                    lotteryLogDTO.setLotteryPrizeName(lotteryPrizeDTO.getLotteryPrizeName());

                    lotteryResultDTO.setLotteryPrizeId(lotteryPrizeDTO.getLotteryPrizeId());
                    lotteryResultDTO.setLotteryPrizeTitle(lotteryPrizeDTO.getLotteryPrizeTitle());
                    lotteryResultDTO.setLotteryPrizeName(lotteryPrizeDTO.getLotteryPrizeName());
                    //中奖方式：2.抽中
                    lotteryResultDTO.setLotteryResultType("2");
                    //组装基础数据
                    lotteryResultDTO.setCrterId(lotteryResultDTO.getTel());
                    lotteryResultDTO.setCrterName(lotteryResultDTO.getPsnName());
                    lotteryResultDTO.setCrteTime(new Date());
                    lotteryResultDTO.setOpterId(lotteryResultDTO.getTel());
                    lotteryResultDTO.setOpterName(lotteryResultDTO.getPsnName());
                    lotteryResultDTO.setOptTime(new Date());
                    break;
                }
            }
        }

        int k = lotteryResultService.insertLotteryResultAndLog(lotteryResultDTO, lotteryLogDTO);
        if (lotteryFlag && k > 0) {
            return success(lotteryResultDTO);
        } else if (lotteryFlag) {
            return error("系统错误，抽奖失败！");
        } else {
            return success("很遗憾您没有中奖");
        }
    }
}
